package com.yunkeji.api.city.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yunkeji.api.city.bo.CityQueryBo;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.dao.domain.SysCity;
import com.yunkeji.dao.mapper.SysCityMapper;
import com.yunkeji.dao.vo.SysCityVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 城市地区相关接口
 *
 * @author 老张
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/city")
public class CityController extends BaseController {

    private final SysCityMapper sysCityMapper;

    /** 获取省市区信息 省level=1 pid=0 ,市level=2 */
    @PostMapping("/getProvince")
    public R<List<SysCityVo>> getProvince(@Valid @RequestBody CityQueryBo bo) {
        List<SysCityVo> cityVos =
                sysCityMapper.selectVoList(
                        Wrappers.lambdaQuery(SysCity.class)
                                .select(
                                        SysCity::getId,
                                        SysCity::getPid,
                                        SysCity::getLevel,
                                        SysCity::getShortname,
                                        SysCity::getName)
                                .eq(bo.getPid() != null, SysCity::getPid, bo.getPid())
                                .eq(bo.getLevel() != null, SysCity::getLevel, bo.getLevel()));
        return R.ok(cityVos);
    }
}
